#include "setcalculatewindow.h"
#include "ui_setcalculatewindow.h"
int compare1(const void* e1, const void *e2)
{
    return  *((int*)e1) - *((int *)e2);
}
SetCalculateWindow::SetCalculateWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SetCalculateWindow)
{
    ui->setupUi(this);
    //初始化成员变量
    this->btn_calculator = ui->btn_calculator;
    this->btn_complex = ui->btn_complex;
    this->btn_matrix = ui->btn_matrix;
    this->textEdit_s1 = ui->textEdit_s1;
    this->textEdit_s2 = ui->textEdit_s2;

    this->textBrowser_result = ui->textBrowser_result;
    this->rbtn_inter = ui->rbtn_inter;
    this->rbtn_union = ui->rbtn_union;
    this->rbtn_subset = ui->rbtn_subset;
    this->btn_clear = ui->btn_clear_2;
    this->btn_get = ui->btn_get_2;

    /* 绑定单击事件 */
    connect(btn_calculator, &QPushButton::clicked, [=]{
        this->hide();
        ApplicationManager::windows[0]->setGeometry(this->geometry());
        ApplicationManager::windows[0]->show();
    });
    connect(btn_complex, &QPushButton::clicked, [=]{
        this->hide();
        ApplicationManager::windows[1]->setGeometry(this->geometry());
        ApplicationManager::windows[1]->show();
    });
    connect(btn_matrix, &QPushButton::clicked, [=]{
        this->hide();
        ApplicationManager::windows[2]->setGeometry(this->geometry());
        ApplicationManager::windows[2]->show();
    });

    connect(btn_clear, &QPushButton::clicked, [=]{
        textEdit_s1->clear();
        textEdit_s2->clear();
        textBrowser_result->setHtml("<p style='font-size:20px'><b>结果展示在这...</b></p>");
    });

    connect(btn_get, &QPushButton::clicked, [=]{
        //清空
        set1.clear();
        set2.clear();
        //获取输入数据
        QString text1 = textEdit_s1->toPlainText();
        QString text2 = textEdit_s2->toPlainText();
        if(text1 == ""){
            textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>请输入数据</b></p>");
            return;
        }
        //结果
        QString resultStr;
        QList<QString> strList;
        //左边输入框输入的数据添加到set1
        QStringList list1 = text1.trimmed().split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
        for(QString line : list1){
            //行向量
            QStringList list = line.trimmed().split(" ");
            for(QString s : list){
                set1.insert(s);
            }
        }
        //求子集只用左边的输入框
        if(rbtn_subset->isChecked()){
            QList<QString> list = set1.values();
            strList = service.subset(list);
            resultStr.append(QString("<h2 style='color:black'>共%1条</h2>").arg(strList.size()));
            for(QString line : strList){
                resultStr.append(line).append("<br>");
            }
            textBrowser_result->setHtml(QString("<p style='color:blue;font-size:18px'>%1</p>").arg(resultStr));
            return;
        }
        //右边输入框输入的数据添加到set2
        if(text2 == ""){
            textBrowser_result->setHtml("<p style='font-size:20px;color:red'><b>请输入数据</b></p>");
            return;
        }
        QStringList list2 = text2.trimmed().split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
        for(QString line : list2){
            //行向量
            QStringList list = line.trimmed().split(" ");
            for(QString s : list){
                set2.insert(s);
            }
        }

//        qDebug()<<set1<<endl;
//        qDebug()<<set2<<endl;

        if(rbtn_inter->isChecked()){
            set1.intersect(set2);
        }else if(rbtn_union->isChecked()){
            set1.unite(set2);
        }

        strList = set1.values();
        resultStr.append(QString("<h2 style='color:black'>共%1条</h2>").arg(strList.size()));
        for(int i = 0; i < strList.size(); i++){
            resultStr.append(strList[i]);
            if(i != 0 && i % 10 == 0){
                resultStr.append("<br>");
            }else{
                resultStr.append("&nbsp;");
            }
        }
        textBrowser_result->setHtml(QString("<p style='color:blue;font-size:18px'>%1</p>").arg(resultStr));
    });
}



SetCalculateWindow::~SetCalculateWindow()
{
    delete ui;
}
